﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;
    using WMS6.Web.UserControls;

    public class AccessorialChargesList : BasePage
    {
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnInvAccessorialCharges;
        protected Button btnList;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F9182;
        protected GridView gvAccessorialCharges;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblBillingService;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2AccessorialChargesEntityList(AccessorialChargesQueryEntity accessorialChargesQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(accessorialChargesQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BillingServiceClientProxy.GetInstance().GetAccessorialChargesByQueryList(accessorialChargesQuery);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                base.BindData2GridView(this.gvAccessorialCharges, ds);
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvAccessorialCharges, "ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9180_001");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9180_002");
            }
            else
            {
                base.Response.Redirect(string.Format("AccessorialCharges.aspx?from=list&ID={0}&Type={1}", base.UrlEncode(selectedID[0]), base.UrlEncode(this.hdnType.Value)));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            accessorialChargesQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = accessorialChargesQuery.CurrentPage;
            accessorialChargesQuery.CurrentPage = "0";
            this.gvAccessorialCharges.Columns[1].Visible = false;
            this.gvAccessorialCharges.AllowSorting = false;
            this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            this.gvAccessorialCharges.ShowFooter = false;
            this.hdnActionID.Value = "";
            accessorialChargesQuery.IsGetAll = false;
            accessorialChargesQuery.CurrentPage = currentPageIndex;
            this.SetAccessorialChargesQueryEntity(accessorialChargesQuery);
            base.Export2Excel("AccessorialCharges.xls", this.gvAccessorialCharges);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            accessorialChargesQuery.CurrentPage = "0";
            this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
        }

        protected void btnInvAccessorialCharges_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("AccessorialChargesDetail.aspx?action={0}&Type=InvCharges&from=list", base.UrlEncode(this.hdnActionID.Value.Trim())), false);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            accessorialChargesQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            accessorialChargesQuery.OrderID = "";
            accessorialChargesQuery.OwnerID = "";
            this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("AccessorialChargesQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            if (int.Parse(accessorialChargesQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                accessorialChargesQuery.CurrentPage = (int.Parse(accessorialChargesQuery.CurrentPage) + 1).ToString();
                this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
            if (int.Parse(accessorialChargesQuery.CurrentPage) > 0)
            {
                accessorialChargesQuery.CurrentPage = (int.Parse(accessorialChargesQuery.CurrentPage) - 1).ToString();
                this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            }
        }

        private AccessorialChargesQueryEntity GetaccessorialChargesQuery()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as AccessorialChargesQueryEntity);
            }
            return (base.PageQueryEntitySession as AccessorialChargesQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
                accessorialChargesQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            }
        }

        protected void gvAccessorialCharges_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9182", this.gvAccessorialCharges);
        }

        protected void gvAccessorialCharges_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvAccessorialCharges_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                AccessorialChargesQueryEntity accessorialChargesQuery = this.GetaccessorialChargesQuery();
                accessorialChargesQuery.SortField = e.SortExpression;
                if (accessorialChargesQuery.SortDirection == "ASC")
                {
                    accessorialChargesQuery.SortDirection = "DESC";
                }
                else if (accessorialChargesQuery.SortDirection == "DESC")
                {
                    accessorialChargesQuery.SortDirection = "ASC";
                }
                this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            AccessorialChargesQueryEntity accessorialChargesQuery = new AccessorialChargesQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    accessorialChargesQuery.OrderID = this.PreviousPage.OrderID;
                    accessorialChargesQuery.OwnerID = this.PreviousPage.OwnerID;
                    accessorialChargesQuery.AccessorialType = this.PreviousPage.AccessorialType;
                    accessorialChargesQuery.CreatedBy = this.PreviousPage.CreatedBy;
                    accessorialChargesQuery.CreatedDate = this.PreviousPage.CreatedDate;
                }
                else
                {
                    accessorialChargesQuery.OrderID = "";
                    accessorialChargesQuery.OwnerID = "";
                    accessorialChargesQuery.CreatedBy = "";
                    accessorialChargesQuery.CreatedDate = "";
                    if (string.IsNullOrEmpty(base.Request.QueryString["AccessorialType"]))
                    {
                        accessorialChargesQuery.AccessorialType = "InBound";
                    }
                    else
                    {
                        accessorialChargesQuery.AccessorialType = base.Request.QueryString["AccessorialType"];
                    }
                }
                this.hdnType.Value = accessorialChargesQuery.AccessorialType;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
                if (this.PreviousPage != null)
                {
                    accessorialChargesQuery.AccessorialType = this.PreviousPage.AccessorialType;
                }
                else if (!string.IsNullOrEmpty(this.hdnType.Value))
                {
                    accessorialChargesQuery.AccessorialType = this.hdnType.Value;
                }
                else
                {
                    accessorialChargesQuery.AccessorialType = "InBound";
                }
                this.hdnType.Value = accessorialChargesQuery.AccessorialType;
            }
            accessorialChargesQuery.IsGetAll = false;
            accessorialChargesQuery.CurrentPage = "0";
            accessorialChargesQuery.PageSize = this.gvAccessorialCharges.PageSize.ToString();
            accessorialChargesQuery.SortField = "R.ROWID";
            accessorialChargesQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvAccessorialCharges.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvAccessorialCharges.Columns[1].Visible = false;
                this.gvAccessorialCharges.Columns[2].Visible = true;
                this.btnEdit.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetAccessorialChargesQueryEntity(accessorialChargesQuery);
            this.BindData2AccessorialChargesEntityList(accessorialChargesQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetAccessorialChargesQueryEntity(AccessorialChargesQueryEntity accessorialChargesQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = accessorialChargesQuery;
            }
            else
            {
                base.PageQueryEntitySession = accessorialChargesQuery;
            }
        }

        public AccessorialChargesQuery PreviousPage
        {
            get
            {
                return (AccessorialChargesQuery) base.PreviousPage;
            }
        }
    }
}

